package com.b1502.store2.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.b1502.store2.R;
import com.b1502.store2.model.GetItems;
import com.b1502.store2.util.ImageLoaderUtils;
import com.b1502.store2.util.UrlUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 购物车适配器
 */

public class GetItemAdapter extends BaseAdapter {
    private GetItems[] getItemses;
    private Context context;
    private CheckInterface mCheckInterface;
    private ModifyCountInterface modifyCountInterface;
    private final DisplayImageOptions displayImageOptions;

    public void setOnMyCheckInterface(CheckInterface mCheckInterface) {
        this.mCheckInterface = mCheckInterface;
    }
    public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
        this.modifyCountInterface = modifyCountInterface;
    }
    public GetItemAdapter(GetItems[] getItemses, Context context) {
        this.getItemses = getItemses;
        this.context = context;
        displayImageOptions = ImageLoaderUtils.initOptions();
    }

    @Override
    public int getCount() {
        return getItemses.length;
    }

    @Override
    public Object getItem(int position) {
        return getItemses[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.cartfragment_getitem, null);
            holder = new ViewHolder();
            holder.namTv = (TextView) convertView.findViewById(R.id.tv_intro_);
            holder.priceTv = (TextView) convertView.findViewById(R.id.tv_price_);
            holder.tv_count_num = (TextView) convertView.findViewById(R.id.tv_count_num);
            holder.ch_CheckBox = (CheckBox) convertView.findViewById(R.id.check_box_);
            holder.imageView = (ImageView) convertView.findViewById(R.id.iv_adapter_list_pic);

            holder.iv_increase = (TextView) convertView.findViewById(R.id.tv_add_);
            holder.iv_decrease = (TextView) convertView.findViewById(R.id.tv_reduce_);
            holder.tv_count = (TextView) convertView.findViewById(R.id.tv_num_);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        GetItems getItemse = getItemses[position];
        holder.namTv.setText(getItemse.getName());
        holder.priceTv.setText("￥" + getItemse.getPrice());
        //复选框监听
        holder.ch_CheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (holder.ch_CheckBox.isChecked()) {
                    getItemses[position].setCheck_(true);
                } else {
                    getItemses[position].setCheck_(false);
                }
                mCheckInterface.checkGroup(position, holder.ch_CheckBox.isChecked());
            }
        });
        //增加
        holder.iv_increase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                modifyCountInterface.doIncrease(position,holder.tv_count,holder.ch_CheckBox.isChecked());
            }
        });
        //减少
        holder.iv_decrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                modifyCountInterface.doDecrease(position,holder.tv_count,holder.ch_CheckBox.isChecked());
            }
        });
        String imageUrl = getItemses[position].getImageUrl();
        ImageLoader.getInstance().displayImage(UrlUtil.getImageUrl(imageUrl)
        ,holder.imageView,displayImageOptions);
        holder.ch_CheckBox.setChecked(getItemses[position].isCheck_());
        holder.tv_count_num.setText("X"+getItemses[position].getCount());
        return convertView;
    }
    class ViewHolder {
        TextView namTv, iv_increase, iv_decrease, tv_count,tv_count_num;
        TextView priceTv;
        ImageView imageView;
        CheckBox ch_CheckBox;
    }
    /**
     * 创建接口
     */
    public interface CheckInterface {
        public void checkGroup(int position, boolean isChecked);
    }

    public interface ModifyCountInterface {
        //增加
        public void doIncrease(int position, View showCountView, boolean isChecked);

        //减少
        public void doDecrease(int position, View showCountView, boolean isChecked);
    }
}
